package keter.framework.web.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class ResponseUtil {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(ResponseUtil.class);

	/**
	 * 默认返回json数据
	 * @param content
	 * @param response 
	 * @author  gulixing@msn.com
	 * @date    2015年12月27日
	 */
	public static void print(String content, HttpServletResponse response) {
		print(content, response, MediaType.APPLICATION_JSON_UTF_8);
	}

	// 向请求端发送返回数据
	public static void printHtml(String content, HttpServletResponse response) {
		print(content, response, MediaType.TEXT_HTML_UTF_8);
	}
	
	private static void print(String content, HttpServletResponse response,
			String contentType) {
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			response.setCharacterEncoding("UTF-8");  
			response.setContentType(contentType);
			response.getWriter().print(content);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (Exception e) {
			logger.error("输出流写入失败！", e);
		}
		if (pw != null) {
			pw.flush();
			pw.close();
		}
	}

}
